unit UltimateSheduler_ConditionForm;

interface

uses
  UltimateSheduler_Defines,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Mask, JvExMask, JvToolEdit, XPMan;

type
  TUSConditionForm = class(TForm)
    Label1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    CBX_Category: TComboBox;
    RB_Category: TRadioButton;
    E_URL: TEdit;
    RB_URL: TRadioButton;
    DE: TJvDirectoryEdit;
    RB_Folder: TRadioButton;
    RB_Filename: TRadioButton;
    E_Filename: TEdit;
    RB_SizeLT: TRadioButton;
    E_Size: TEdit;
    RB_SizeGT: TRadioButton;
    RB_SizeEQ: TRadioButton;
    FE: TJvFilenameEdit;
    RB_Flag: TRadioButton;
    RB_CategoryHas: TRadioButton;
    XPManifest1: TXPManifest;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CBX_CategoryChange(Sender: TObject);
    procedure E_URLChange(Sender: TObject);
    procedure DEChange(Sender: TObject);
    procedure E_FilenameChange(Sender: TObject);
    procedure E_SizeChange(Sender: TObject);
    procedure FEChange(Sender: TObject);
    procedure RB_CategoryClick(Sender: TObject);
    procedure RB_URLClick(Sender: TObject);
    procedure RB_FolderClick(Sender: TObject);
    procedure RB_FilenameClick(Sender: TObject);
    procedure RB_SizeLTClick(Sender: TObject);
    procedure RB_SizeGTClick(Sender: TObject);
    procedure RB_SizeEQClick(Sender: TObject);
    procedure RB_FlagClick(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormDestroy(Sender: TObject);
    procedure RB_CategoryHasClick(Sender: TObject);
  private
    { Private declarations }
    FCondition: Condition;
    procedure BitBtnClick(BitBtn: TBitBtn);
    procedure SetLanguage(language: String);
    procedure SetCondition(ACondition: Condition);
    procedure SetCategories;
    procedure CheckCondition(Other: Boolean = true);
  public
    { Public declarations }
    property Language: String write SetLanguage;
    property myCondition: Condition read FCondition write SetCondition;
  end;

implementation

{$R *.dfm}

uses
 gnugettext,
 Locales,
 RegExpr;

var
 CanSetCond: Boolean = true;

procedure TUSConditionForm.BitBtnClick(BitBtn: TBitBtn);
begin
 Self.ModalResult:=BitBtn.ModalResult;
 if not (fsModal in Self.FormState) then
   Self.Close;
end;

procedure TUSConditionForm.BitBtn1Click(Sender: TObject);
begin
 BitBtnClick(BitBtn1);
end;

procedure TUSConditionForm.BitBtn2Click(Sender: TObject);
begin
 BitBtnClick(BitBtn2);
end;

procedure TUSConditionForm.SetLanguage(language: String);
begin
 TranslateForm(Self, Language);
end;

procedure TUSConditionForm.SetCondition(ACondition: Condition);
begin
 FCondition.Assign(ACondition);
 //
 CanSetCond:=false;
 CheckCondition(false);
 CanSetCond:=true;
end;

procedure TUSConditionForm.SetCategories;
 var
  i: Integer;
begin
 CBX_Category.Items.Clear;
 for i:=0 to CatList.Count-1 do
   CBX_Category.Items.Add(CatList.ValueFromIndex[i]);
end;

procedure TUSConditionForm.FormCreate(Sender: TObject);
begin
 TranslateComponent(Self);
 //
 FCondition:=Condition.Create(nil);
 //
 SetCategories;
end;

procedure TUSConditionForm.CheckCondition(Other: Boolean = true);
 var
  i: Integer;
 procedure SetEdit(Edit: TEdit; AText: String; Clear: Boolean = false);
 begin
  if Clear then
    Edit.Text:=''
  else
    Edit.Text:=AText;
 end;
begin
 RB_Category.Checked:=FCondition.CondType=cndCategory;
 RB_CategoryHas.Checked:=FCondition.CondType=cndCategoryHas;
 CBX_Category.Enabled:=RB_Category.Checked or RB_CategoryHas.Checked;
 if CBX_Category.Enabled then
  begin
   if Other then
     i:=0
   else
    begin
     i:=IndexOfValue(FCondition.Parameter, CBX_Category.Items);
     if i<0 then
       i:=0;
    end;
   CBX_Category.ItemIndex:=i;
   if i=0 then
     FCondition.Parameter:=CBX_Category.Text;
  end
 else
   CBX_Category.ItemIndex:=0;
 //
 RB_URL.Checked:=FCondition.CondType=cndURL;
 E_URL.Enabled:=RB_URL.Checked;
 //ShowMessage(FCondition.Parameter); //debug
 if E_URL.Enabled then
   SetEdit(E_URL, FCondition.Parameter, Other)
 else
   SetEdit(E_URL, '');
 //
 RB_Folder.Checked:=FCondition.CondType=cndFolder;
 DE.Enabled:=RB_Folder.Checked;
 if DE.Enabled then
  begin
   if Other then
     DE.Text:=''
   else
     DE.Text:=FCondition.Parameter;
  end
 else
   DE.Text:='';
 //
 RB_Filename.Checked:=FCondition.CondType=cndFilename;
 E_Filename.Enabled:=RB_Filename.Checked;
 if E_Filename.Enabled then
   SetEdit(E_Filename, FCondition.Parameter, Other)
 else
   SetEdit(E_Filename, '');
 //
 RB_SizeLT.Checked:=FCondition.CondType=cndSizeLT;
 //RB_SizeLE.Checked:=FCondition.CondType=cndSizeLE;
 RB_SizeGT.Checked:=FCondition.CondType=cndSizeGT;
 //RB_SizeGE.Checked:=FCondition.CondType=cndSizeGE;
 RB_SizeEQ.Checked:=FCondition.CondType=cndSizeEQ;
 E_Size.Enabled:=RB_SizeLT.Checked{ or RB_SizeLE.Checked} or RB_SizeGT.Checked{ or RB_SizeGE.Checked} or RB_SizeEQ.Checked;
 if E_Size.Enabled then
   SetEdit(E_Size, FCondition.Parameter, Other)
 else
   SetEdit(E_Size, '');
 //
 RB_Flag.Checked:=FCondition.CondType=cndFlagExists;
 //RB_FlagNo.Checked:=FCondition.CondType=cndFlagDontExists;
 FE.Enabled:=RB_Flag.Checked{ or RB_FlagNo.Checked};
 if FE.Enabled then
  begin
   if Other then
     FE.Text:=''
   else
     FE.Text:=FCondition.Parameter;
  end
 else
   FE.Text:='';
end;

procedure TUSConditionForm.CBX_CategoryChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=CBX_Category.Text;
end;

procedure TUSConditionForm.E_URLChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=E_URL.Text;
end;

procedure TUSConditionForm.DEChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=TrimQuotes(DE.Text);
end;

procedure TUSConditionForm.E_FilenameChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=E_Filename.Text;
end;

procedure TUSConditionForm.E_SizeChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=E_Size.Text;
end;

procedure TUSConditionForm.FEChange(Sender: TObject);
begin
 if CanSetCond then
   FCondition.Parameter:=FE.Text;
end;

procedure TUSConditionForm.RB_CategoryClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndCategory;
   CheckCondition();
  end;
end;

procedure TUSConditionForm.RB_URLClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndURL;
   CheckCondition();
  end;
end;

procedure TUSConditionForm.RB_FolderClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndFolder;
   CheckCondition();
  end;
end;

procedure TUSConditionForm.RB_FilenameClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndFilename;
   CheckCondition();
  end;
end;

procedure TUSConditionForm.RB_SizeLTClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndSizeLT;
   CheckCondition();
  end;
end;

procedure TUSConditionForm.RB_SizeGTClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndSizeGT;
   CheckCondition();
  end;
end;

procedure TUSConditionForm.RB_SizeEQClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndSizeEQ;
   CheckCondition();
  end;
end;

procedure TUSConditionForm.RB_FlagClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndFlagExists;
   CheckCondition();
  end;
end;

procedure TUSConditionForm.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
 var
  re: TRegExpr;
  i: Integer;
begin
 re:=TRegExpr.Create;
 re.ModifierI:=true; //case insensitive search
 if ModalResult=mrOK then
  begin
   if FCondition.CondType=0 then
    begin
     MessageDlg(_('Please select type of condition!'), mtError, [mbOK], 0);
     CanClose:=false;
     Exit;
    end;
   if E_URL.Enabled then
    begin
     re.Expression:=E_URL.Text;
     try
      CanClose:=true;
      re.Compile;
     except
      CanClose:=false;
      raise;
     end;
     Exit;
    end;
   if E_Filename.Enabled then
    begin
     re.Expression:=E_Filename.Text;
     try
      CanClose:=true;
      re.Compile;
     except
      CanClose:=false;
      raise;
     end;
     Exit;
    end;
   if E_Size.Enabled then
    begin
     i:=StrToIntDef(E_Size.Text, -1);
     if i<0 then
      begin
       MessageDlg(_('Size value must be 0 or greater!'), mtError, [mbOK], 0);
       ActiveControl:=E_Size;
       CanClose:=false;
       Exit;
      end;
    end;
   //set external function flag
   FCondition.ExternalFlag:=FCondition.CondType=cndCategoryHas;
  end;
 FreeAndNil(re);
end;

procedure TUSConditionForm.FormDestroy(Sender: TObject);
begin
 FreeAndNil(FCondition);
end;

procedure TUSConditionForm.RB_CategoryHasClick(Sender: TObject);
begin
 if CanSetCond then
  begin
   FCondition.CondType:=cndCategoryHas;
   CheckCondition();
  end;
end;

end.
